<style>
    .layui-form .layui-form-item .layui-inline{
        padding-left: 5px;
    }
    .layui-form .layui-form-item .layui-inline input{
        width: 180px;
    }
</style>
<form class="layui-form seller-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <input type="text" name="user_name" lay-verify="title" placeholder="用户名" autocomplete="off"
                   class="layui-input">
        </div>
        <div class="layui-inline">
            <input type="text" name="value" lay-verify="title" placeholder="充值码" autocomplete="off"
                   class="layui-input">
        </div>
        <div class="layui-inline">
            <select name="is_used" >
                <option value="">使用状态</option>
                <option value="0">未使用</option>
                <option value="1">已使用</option>
            </select>
        </div>
        <div class="layui-inline">
            <select name="amount" >
                <option value="">卡金额</option>
                <option value="50.00">50.00</option>
                <option value="100.00">100.00</option>
            </select>
        </div>
        <div class="layui-inline">
            <input type="text" name="datetime" id="datetime" placeholder="start_time _ end_time" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-inline">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="search"><i class="iconfont icon-chaxun"></i>Search</button>
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="create_50">生成50张50.00</button>
<!--                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="create_100">生成50张100.00</button>-->
            </div>
        </div>

    </div>
</form>
<div class="table-body">
    <table id="balanceTable" lay-filter="balanceTable"></table>
</div>
<script>
    layui.use(['form', 'laydate', 'table',], function () {
        var laydate = layui.laydate,
            form = layui.form,
            table = layui.table; //表格
        //时间插件
        laydate.render({
            elem: '#datetime',
            range: '_',
            format: 'yyyy-MM-dd'
        });
        var tableIn = table.render({
            elem: '#balanceTable',
            height: 'full-99',
            cellMinWidth: '80',
            totalRow: true,
            method: 'post',
            url: "{:url('balance/fundCard')}",
            toolbar: '#toolbar', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports'],
            id: 'balanceTable',
            cols: [[
                { type: 'numbers'},
                { field: 'username',  align: 'left', title: '充值码使用人',width: 200},
                { field: '', align: 'center', title: '充值码状态' ,width:120,
                    templet: function (d) {
                        const is_used = d.is_used;
                        if(is_used == 1){
                            return '<font color="green">已使用</font>';
                        }else{
                            return '<font color="gray">未使用</font>';
                        }
                    },totalRowText:'Total:'
                },
                { field: 'amount', title: '金额($)', align:"right",width:120,
                    style:'color:#FF5722;font-weight:bold',totalRow: true},
                { field: 'value',  align: 'left', title: '充值码'},
                { field: 'pwd',  align: 'left', title: '密码'},
                { field: 'ctime',align: 'center', title: '创建时间' , width: 160,
                    templet: function (d) {
                        return layui.util.toDateString(d.ctime * 1000, "yyyy-MM-dd HH:mm:ss");
                    }
                },
                { field: 'utime',align: 'center', title: '使用时间' , width: 160,
                    templet: function (d) {
                        var utime = d.utime;
                        if(utime == null){
                            return ' -- ';
                        }else{
                            return layui.util.toDateString(utime * 1000, "yyyy-MM-dd HH:mm:ss");
                        }
                    }
                }
            ]],
            page: true,
            limit: 20,
            limits: [20,  100 , 500 , 1000 ,3000]
        });
        form.on('submit(search)', function (data) {
            layui.table.reload('balanceTable', {
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        layui.form.on('submit(create_50)', function () {
            var loading =layer.load(1, {shade: [0.1,'#fff']});
            JsPost("{:url('createCard')}",{type_amount:0}, function(res){
                layer.close(loading);
                if(res.code >0){
                    layer.msg(res.msg , {time:1500 , icon:1});
                    setTimeout("window.location.href='" + "{:url('balance/fundCard')}" + "'; ", 1000);
                }else{
                    layer.msg(res.msg , {time:1500 , icon:2});
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        layui.form.on('submit(create_100)', function () {
            var loading =layer.load(1, {shade: [0.1,'#fff']});
            JsPost("{:url('createCard')}",{type_amount:1}, function(res){
                layer.close(loading);
                if(res.code >0){
                    layer.msg(res.msg , {time:1500 , icon:1});
                    setTimeout("window.location.href='" + "{:url('balance/fundCard')}" + "'; ", 1000);
                }else{
                    layer.msg(res.msg , {time:1500 , icon:2});
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //余额编辑
        $(document).on('click', '.editRemark', function () {
            var id = $(this).attr('data-id');
            JsGet("{:url('fundsRemark')}?id="+id, function(e){
                if(e.status == 1){
                    layer.open({
                        type: 1,
                        title: 'Edit Remark',
                        area: ['500px', '466px'], //宽高
                        content: e.datas,
                        btn:['Confirm','Close'],
                        yes:function (index,layero) {
                            var thedata = $('.operationForm').serialize();
                            var remark   = $("#remark").val();
                            if (remark == '' || !remark) {
                                layer.msg("Please input in Remark" , {icon:2,time:1500});
                                return false;
                            }
                            JsPost('{:url("fundsRemark")}', thedata,function (re) {
                                if(re.code >0){
                                    layer.msg(re.msg , {icon:1,time:1500});
                                    layer.close(index);
                                    tableIn.reload({
                                        page:{
                                            curr :1
                                        }
                                    });
                                }else{
                                    layer.msg(re.msg, {icon:2,time:1500});
                                }
                            });
                        }
                    });
                }else{
                    layer.msg(e.msg);
                }
            });
        });
    });
</script>